CMU 15-112 Summer 2019: Fundamentals of Programming and Computer Science
Homework 3 (Due Sun 26-May, at 5pm)
- This assignment is SOLO. This means you may not look at other student's code or let other students look at your code for these problems. See the syllabus for details.
- To start:
- Create a folder named 'week1'
- Download hw3.py to that folder
- Edit hw3.py using Pyzo
- When you are ready, submit hw3.py to Autolab. For this hw, you may submit up to 20 times (which is way more than you should require), but only your last submission counts.
- Do not use lists or recursion in this assignment.
- Do not hardcode the test cases in your solutions.
- Feedback Form [5 pts]
Fill out this form to let us know how we are doing. This form is anonymous. Please take time to give us meaningful feedback. We want to make this class the best it can be for you! At the end, we will ask you to fill out another form so you get credit on hw3. We won't be able to match up the submissions. - Piazza Post [5 pts]
Ask a question on Piazza about the homework or the course (if you already did in the last week, you don't have to do this again!) - areAnagrams(s1, s2) [10 pts]
Write the function areAnagrams(s1, s2) that takes two strings, s1 and s2, that you may assume contain only upper and/or lower case letters, and returns True if the strings are anagrams, and False otherwise. Two strings are anagrams if each can be reordered into the other. Treat "a" and "A" as the same letters (so "Aba" and "BAA" are anagrams). You may not use sort() or sorted() or any other list-based functions or approaches. Hint: you may use s.count(), which could be quite handy here. - bestStudentAndAvg(gradebook) [30 pts]
Background: for this problem, a "gradebook" is a multiline string where each row contains a student's name (one word, all lowercase) followed by one or more comma-separated integer grades. A gradebook always contains at least one student, and each row always contains at least one grade. Gradebooks can also contain blank lines and lines starting with the "#" character, which should be ignored.
With this in mind, write the function bestStudentAndAvg(gradebook), that takes a gradebook and finds the student with the best average (ignoring the case where there is a tie) and returns a string of that student's name followed by a colon (":") followed by his/her average (rounded using the provided roundHalfUp). For example, here is a test case:gradebook = """ # ignore blank lines and lines starting with #'s wilma,91,93 fred,80,85,90,95,100 betty,88 """ assert(bestStudentAndAvg(gradebook) == "wilma:92"))
Note: you most likely will want to use both s.split(",") and s.splitlines() in your solution. - encodeColumnShuffleCipher(message, key) [30 pts]
Background: In this problem you will implement a simple encryption cipher we've called a column shuffle cipher. It takes two values, a plaintext and a key, and it constructs a grid of the letters of the message, rearranges the columns of the grid, and then reads the characters back column by column.
Consider the following example:
Function call: encodeColumnShuffleCipher("ILOVECMUSOMUCH", "021") Message: ILOVECMUSOMUCH Key: 021 Initial Grid: Rearranged Grid: I L O I O L V E C V C E M U S M S U O M U O U M C H - C - H Encrypted Message: IVMOCOCSU-LEUMH Message to Return: 021IVMOCOCSU-LEUMH
The first step is to take the message and arrange it into a grid that has as many columns as the key has characters. (In this case, 3 columns.) Any empty spaces at the end are filled with - characters. We then rearrange the grid by changing the column order to match the order specified by the key. In this case, the 0th column becomes first, then the 2nd column, then the 1st. Finally, we read out the encrypted message by reading down the columns from left to right. The returned value from the function is just the encrypted message prepended with the key.
With this in mind, write the function encodeColumnShuffleCipher(message, key) that takes an all-uppercase message and a valid key, and returns the encoding as just described.
Hint: In your program you won't actually build a grid. Instead, you will use the concept of the grid to help you calculate the index of individual characters in your message. - decodeColumnShuffleCipher(encodedMessage) [20 pts]
Write the function decodeColumnShuffleCipher(encodedMessage), which takes an encoding from encodeColumnShuffleCipher and runs it in reverse, returning the plaintext that generated the encoding. For example, decodeColumnShuffleCipher("0213WTAWACD-EATNTKA-") returns "WEATTACKATDAWN".